import { createRouter, createWebHistory } from 'vue-router'
import LayOut from '@/LayOut'
import { isLogin, getRole } from '@/utils'

const routes = [
  {
    path: '/',
    name: '首页',
    component: LayOut,
    children: [
      {
        path: '/',
        redirect: '/home'

      },
      {
        path: '/home',
        name: '首页',
        meta: {
          roles: ['admin', 'a', 'b']
        },
        component: () => import('@v/HomePage')
      },
      /* {
        path: '/cateLists',
        name: '食品分类',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        component: () => import('@v/CateLists')
      },
      {
        path: '/foodLists',
        name: '食品管理',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        component: () => import('@v/FoodLists')
      },
      {
        path: '/addShops',
        name: '添加商铺',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        component: () => import('@v/AddShops')
      }, */
      {
        path: '/userInfo',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        name: '个人中心',
        component: () => import('@v/UserInfo')
      },
      {
        path: '/role',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        name: '角色管理',
        component: () => import('@v/RoleManage')
      },
      {
        path: '/setting',
        meta: {
          roles: ['superAdmin', 'a', 'b']
        },
        name: '设置',
        component: () => import('@v/SettingPage')
      }
    ]
  },
  {
    path: '/manage',
    name: '业务管理',
    component: LayOut,
    children: [
      {
        path: '/',
        redirect: '/manage'

      },
      {
        path: '/cateLists',
        name: '食品分类',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        component: () => import('@v/CateLists')
      },
      {
        path: '/foodLists',
        name: '食品管理',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        component: () => import('@v/FoodLists')
      },
      {
        path: '/addShops',
        name: '添加商铺',
        meta: {
          roles: ['superAdmin', 'admin', 'a', 'b']
        },
        component: () => import('@v/AddShops')
      }
    ]
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@v/LoginPage')
  },
  {
    path: '/:pathMatch(.*)*',
    name: 'notFound',
    component: () => import('@v/NotFound')
  },
  {
    path: '/noauth',
    meta: {
      roles: ['*']
    },
    name: 'noauth',
    component: () => import('@v/NoPermission')
  }
]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

router.beforeEach((to, from, next) => {
  const role = getRole()
  if (to.path !== '/login') {
    if (isLogin()) {
      // 判断当前用户是否有权限访问该路由
      if (to.meta.roles.includes(role) || to.meta.roles.includes('*')) {
        next()
      } else {
        next('/noauth')
      }
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router
